Page({
    data: {
        contact_tel: "",
        show_customer_service: 0,
        showDialog: 1,
        show_copyright: false,
        clerk_dialog: false,
        clerk_code: '',
        statusbar: 0,
        headshow: 0,
        titleH:0
    },
    onLoad: function(e) {
        const res = wx.getSystemInfoSync();
        var statusbarH = res.statusBarHeight;
        const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
        var titleH = (menuButtonInfo.top - statusbarH) * 2 + menuButtonInfo.height;
        this.setData({
        statusbar: statusbarH,
        titleH:titleH
        })
        getApp().page.onLoad(this, e);
    },
    onPageScroll: function (e) {
        let opacity = 0
        if (e.scrollTop <= 200) {
          opacity = e.scrollTop / 200
        } else {
          opacity = 1
        }
        this.setData({
          headshow: opacity
        })
    },
    goBack() {
        wx.navigateBack({
          delta: 1
        })
    },
    onClickClerkDialogCancel: function () {
        this.setData({
            clerk_code: '',
            clerk_dialog: false
        })
    },
    onClickClerkDialogOk: function () {
        var code = this.data.clerk_code
        var self = this;
        if (code.length == 0) {
            wx.showToast({
                title: '请填写核销码',
                icon: 'error',
                duration: 2000
            });
            return;
        }
        wx.showLoading({
            title: '加载中',
        })
        getApp().request({
            url: getApp().api.verify.verify_code,
            data: {code: code},
            success: function(res) {
                if (res.code == 0) {
                    self.setData({
                        clerk_code: '',
                        clerk_dialog: false
                    })
                    wx.navigateTo({
                        url: '/' + res.data.path + '?scene=' + encodeURIComponent(res.data.scene)
                    })
                } else {
                    wx.showModal({
                        title: '提示',
                        showCancel: false,
                        content: res.msg,
                        success (res) {}
                    });
                }
            },
            complete: () => {
                wx.hideLoading()
            }
        });
    },
    onBindInput: function (e) {
        this.setData({
            clerk_code: e.detail.value
        })
    },
    loadData: function(e) {
        var t = this;
        wx.showLoading({
          title: '加载中',
        })
        t.setData({
            show_copyright: false,
            store: getApp().core.getStorageSync(getApp().const.STORE)
        }), getApp().request({
            url: getApp().api.user.index,
            success: function(o) {
                if (0 == o.code) {
                    if ("my" == t.data.__platform) o.data.menus.forEach(function(e, t, a) {
                        "bangding" === e.id && o.data.menus.splice(t, 1, 0);
                    });
                    t.setData(o.data), getApp().core.setStorageSync(getApp().const.PAGES_USER_USER, o.data), 
                    getApp().core.setStorageSync(getApp().const.SHARE_SETTING, o.data.share_setting), 
                    getApp().core.setStorageSync(getApp().const.USER_INFO, o.data.user_info);
                }
            },
            complete: () => {
                // console.log(getApp().core.getStorageSync(getApp().const.USER_INFO))
                // getApp().core.getStorage({
                //     key: "USER_INFO",
                //     success: function(a) {
                //         console.log(a)
                        
                //     }
                // });
                wx.hideLoading()
                t.setData({
                    show_copyright: true
                })
            }
        });
    },
    onReady: function(e) {
        getApp().page.onReady(this);
    },
    onShow: function(e) {
        getApp().page.onShow(this);
        this.loadData();
    },

    callTel: function(e) {
        var t = e.currentTarget.dataset.tel;
        getApp().core.makePhoneCall({
            phoneNumber: t
        });
    },
    apply: function(t) {
        var a = getApp().core.getStorageSync(getApp().const.SHARE_SETTING), o = getApp().getUser();
        1 == a.share_condition ? getApp().core.navigateTo({
            url: "/pages/add-share/index"
        }) : 0 != a.share_condition && 2 != a.share_condition || (0 == o.is_distributor ? getApp().core.showModal({
            title: "申请成为" + this.data.store.share_custom_data.words.share_name.name,
            content: "是否申请？",
            success: function(e) {
                e.confirm && (getApp().core.showLoading({
                    title: "正在加载",
                    mask: !0
                }), getApp().request({
                    url: getApp().api.share.join,
                    method: "POST",
                    data: {
                        form_id: t.detail.formId
                    },
                    success: function(e) {
                        0 == e.code && (0 == a.share_condition ? (o.is_distributor = 2, getApp().core.navigateTo({
                            url: "/pages/add-share/index"
                        })) : (o.is_distributor = 1, getApp().core.navigateTo({
                            url: "/user/share/index"
                        })), getApp().core.setStorageSync(getApp().const.USER_INFO, o));
                    },
                    complete: function() {
                        getApp().core.hideLoading();
                    }
                }));
            }
        }) : getApp().core.navigateTo({
            url: "/pages/add-share/index"
        }));
    },
    verify: function(e) {
        getApp().core.scanCode({
            onlyFromCamera: !1,
            success: function(e) {
                getApp().core.navigateTo({
                    url: "/" + e.path
                });
            },
            fail: function(e) {
                getApp().core.showToast({
                    title: "失败"
                });
            }
        });
    },
    member: function() {
        getApp().core.navigateTo({
            url: "/pages/member/member"
        });
    },
    integral_mall: function(e) {
        var t, a;
        getApp().permission_list && getApp().permission_list.length && (t = getApp().permission_list, 
        a = "integralmall", -1 != ("," + t.join(",") + ",").indexOf("," + a + ",")) && getApp().core.navigateTo({
            url: "/pages/integral-mall/index/index"
        });
    },
    onClickClerk: function () {
        var self = this;
        wx.showActionSheet({
            itemList: ['扫码核销', '输入核销码'],
            success (res) {
                var index = res.tapIndex;
                if (index == 0) {
                    // 扫码
                    wx.scanCode({
                        success(res) {
                            wx.navigateTo({
                                url: '/' + res.path
                            })
                        }
                    })
                }
                if (index == 1) {
                    // 输入核销码
                    self.setData({
                        clerk_dialog: true
                    })
                }
            },
            fail (res) {
              console.log(res.errMsg)
            }
        })
    },
    //调用扫码接口
    onAddCart: function () {
        wx.scanCode({
            success: (res) => {
                var no = res.result;
                getApp().request({
                    url: getApp().api.cart.find_goods,
                    data: {
                        no: no
                    },
                    method: 'POST',
                    success: function (res) {
                        if (res.code == 1) {
							wx.showModal({
                                title: '提示',
                                content: res.msg,
                                showCancel: false
                            })
                            return;
						}
                        //查询到商品后执行添加线下购物车操作
                        var id = res.data.id;
                        var str = res.data.return_str;
                        getApp().request({
                            url: getApp().api.cart.add_cart_offline,
                            data: {
                                goods_id: id,
                                attr: str,
                                num: 1
                            },
                            method: 'POST',
                            success: function (res) {
                                wx.showToast({
                                    title: res.msg,
                                })
                                //跳转到线下购物车页面
                                if (res.code == 0) {
                                    getApp().core.navigateTo({
                                        url: "/pages/cartoffline/cartoffline"
                                    });
                                }

                            }
                        });
                    }
                });
            }
        })
    },
    clearCache: function() {
        wx.showActionSheet({
            itemList: [ "清除缓存" ],
            success: function(e) {
                if (0 === e.tapIndex) {
                    wx.showLoading({
                        title: "清除中..."
                    });
                    getApp().getStoreData();
                    setInterval(function() {
                        wx.hideLoading();
                    }, 1e3);
                }
            }
        });
    },
    completemessage: function(e) {
        var t = this.data.__wxapp_img.cell, a = [ t.cell_1.url, t.cell_2.url, t.cell_3.url, t.cell_4.url, t.cell_5.url ];
        getApp().core.previewImage({
            current: a[0],
            urls: a
        });
    },
    extract: function () {
      this.setData({
        showDialog: !this.data.showDialog
      });
  },
  bindingPhone: function () {
    getApp().core.navigateTo({
      url: "/pages/bangding/bangding"
    })
  },
  clickskip(){
    wx.navigateTo({
      url: '/vipcard/index/index',
    })
  },
  toEditmsg: function () {
    getApp().core.navigateTo({
      url: "/weitao/personal/personal"
    })
  }
});